﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;

namespace Controls
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TextBox_SelectionChanged(object sender, RoutedEventArgs e)
        {
            TextBlock.Text = TextBoxMain.SelectedText;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (LabelCounter.Content==null)
            {
                LabelCounter.Content = 0;
            }
            LabelCounter.Content = int.Parse(LabelCounter.Content.ToString())+1;
        }

        private void ButtonSubmitRadio_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in StackPanelEx3.Children)
            {
                if (item is RadioButton)
                {
                    var rad = item as RadioButton;
                    if (rad.IsChecked==true)
                    {
                        MessageBox.Show(rad.Content.ToString());
                    }
                }
                if (item is CheckBox)
                {
                    var check = item as CheckBox;
                    if (check.IsChecked==true)
                    {
                        MessageBox.Show(check.Content.ToString());
                    }
                }
            }
        }

        private void CommandBinding_Executed_Save(object sender, ExecutedRoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".text"; // Default file extension
            dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

            // Show save file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process save file dialog box results
            if (result == true)
            {
                // Save document
                string filename = dlg.FileName;
                using (var writer = new StreamWriter(filename))
                {
                    writer.WriteLine(TextBoxInput.Text);
                }

            }

        }

        private void CommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void CommandBinding_Executed_Open(object sender, ExecutedRoutedEventArgs e)
        {
            // Configure open file dialog box
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.FileName = "Document"; // Default file name
            dlg.DefaultExt = ".txt"; // Default file extension
            dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                // Open document
                string filename = dlg.FileName;
                StreamReader reader = new StreamReader(filename,Encoding.GetEncoding("Windows-1251"));
                using (reader)
                {
                    var wholeText = reader.ReadToEnd();
                    TextBoxInput.Text = wholeText;
                }
            }
        }

        private void ComboBoxItem_Selected(object sender, RoutedEventArgs e)
        {

        }
    }
}
